package com.kjtpay.design_pattern.iterator;

import java.util.ArrayList;

class ConcreteAggregate extends Aggregate {
    private ArrayList<Object> items = new ArrayList<Object>();

    // 集合个数，只提供getter
    public int count;

    @Override
    public Iterator createIterator() {
        return new ConcreteIterator(this);
    }

    // 通过下标访问元素
    public Object objectAtIndex(int index) {
        if (index < count) {
            return items.get(index);
        } else {
            return null;
        }
    }

    // 添加元素
    public void add(Object object) {
        items.add(object);
        count = items.size();
    }
}
